package com.sls.web.manager.action.game;

import java.util.List;

import javax.annotation.Resource;

import com.sls.web.manager.action.SuperAction;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.common.FilterStringSpaces;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.game.pojo.EnfuInfo;
import com.sls.web.manager.game.querybean.EnfuQuery;
import com.sls.web.manager.game.service.EnfuService;
import com.sls.web.manager.page.Pagination;

/**
 * 
 * @description 恩付action
 * @author Wangxuegang
 * @date 2013-5-4
 */
public class EnfuAction extends SuperAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private EnfuInfo enfuInfo;

	private EnfuQuery enfuQuery;

	private List<EnfuInfo> merchantidList;

	@Resource(name = "enfuService")
	private EnfuService enfuService;

	private int priceRatio;
	private String supplierId;
	private Pagination<EnfuInfo> page;
	private String orderno;
	private String id;

	// ---------------------------------------------------

	/**
	 * 小服分页展示
	 * 
	 * @return
	 * @throws Exception
	 */
	public String srvList() throws Exception {
		try {
			if (enfuQuery == null) {
				enfuQuery = new EnfuQuery();
				enfuQuery.setPageSize(Constants.INIT_PAGESIZE);
				enfuQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
			}
			String productid = enfuQuery.getProductid();
			String areaname = enfuQuery.getAreaname();
			String srvname = enfuQuery.getSrvname();
			enfuQuery.setProductid(productid);
			enfuQuery.setAreaname(areaname);
			enfuQuery.setSrvname(srvname);
			page = enfuService.queryByPageOfSrv(enfuQuery, getCurrentOperator());
		} catch (ManagerException e) {
			addActionError(e.getMessage());
		}
		return "srv_list";
	}

	/**
	 * 大区分页展示
	 * 
	 * @return
	 * @throws Exception
	 */
	public String areaList() throws Exception {
		try {
			if (enfuQuery == null) {
				enfuQuery = new EnfuQuery();
				enfuQuery.setPageSize(Constants.INIT_PAGESIZE);
				enfuQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
			}
			String productid = enfuQuery.getProductid();
			String gamename = enfuQuery.getGamename();
			String areaname = enfuQuery.getAreaname();
			productid = FilterStringSpaces.checkStringHasSpace(productid);
			gamename = FilterStringSpaces.checkStringHasSpace(gamename);
			areaname = FilterStringSpaces.checkStringHasSpace(areaname);
			enfuQuery.setProductid(productid);
			enfuQuery.setGamename(gamename);
			enfuQuery.setAreaname(areaname);
			page = enfuService.queryByPageOfArea(enfuQuery,
					getCurrentOperator());
		} catch (ManagerException e) {
			addActionError(e.getMessage());
		}
		return "area_list";
	}

	/**
	 * 确认审核
	 * 
	 * @return
	 * @throws Exception
	 */
	public String editProduct() throws Exception {
		try {
			id = FilterStringSpaces.checkStringHasSpace(id);
			enfuService.confirmAudit(id, getCurrentOperator());

			if (enfuQuery == null) {
				enfuQuery = new EnfuQuery();
				enfuQuery.setPageSize(Constants.INIT_PAGESIZE);
				enfuQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
			}
			String productid = enfuQuery.getProductid();
			String productname = enfuQuery.getProductname();
			String definedproductid = enfuQuery.getDefinedproductid();
			String gamename = enfuQuery.getGamename();
			productid = FilterStringSpaces.checkStringHasSpace(productid);
			productname = FilterStringSpaces.checkStringHasSpace(productname);
			definedproductid = FilterStringSpaces
					.checkStringHasSpace(definedproductid);
			gamename = FilterStringSpaces.checkStringHasSpace(gamename);
			enfuQuery.setProductid(productid);
			enfuQuery.setProductname(productname);
			enfuQuery.setDefinedproductid(definedproductid);
			enfuQuery.setGamename(gamename);

			String producttype = enfuQuery.getProducttype();
			if ("".equals(producttype)) {
				producttype = null;
				enfuQuery.setProducttype(producttype);
			}
			String isAudit = enfuQuery.getIsAudit();
			if ("".equals(isAudit)) {
				isAudit = null;
				enfuQuery.setIsAudit(isAudit);
			}
			page = enfuService.queryByPageOfGoodsDetail(enfuQuery,
					getCurrentOperator());
		} catch (ManagerException e) {
			addActionError(e.getMessage());
		}
		return "goodsdetail_list";
	}

	/**
	 * 商品详细列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String goodsDetailList() throws Exception {
		try {
			if (enfuQuery == null) {
				enfuQuery = new EnfuQuery();
				enfuQuery.setPageSize(Constants.INIT_PAGESIZE);
				enfuQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
			}
			String productid = enfuQuery.getProductid();
			String productname = enfuQuery.getProductname();
			String definedproductid = enfuQuery.getDefinedproductid();
			String gamename = enfuQuery.getGamename();
			productid = FilterStringSpaces.checkStringHasSpace(productid);
			productname = FilterStringSpaces.checkStringHasSpace(productname);
			definedproductid = FilterStringSpaces
					.checkStringHasSpace(definedproductid);
			gamename = FilterStringSpaces.checkStringHasSpace(gamename);
			enfuQuery.setProductid(productid);
			enfuQuery.setProductname(productname);
			enfuQuery.setDefinedproductid(definedproductid);
			enfuQuery.setGamename(gamename);

			String producttype = enfuQuery.getProducttype();
			if ("".equals(producttype)) {
				producttype = null;
				enfuQuery.setProducttype(producttype);
			}
			String isAudit = enfuQuery.getIsAudit();
			if ("".equals(isAudit)) {
				isAudit = null;
				enfuQuery.setIsAudit(isAudit);
			}
			page = enfuService.queryByPageOfGoodsDetail(enfuQuery,
					getCurrentOperator());
		} catch (ManagerException e) {
			addActionError(e.getMessage());
		}
		return "goodsdetail_list";
	}

	/**
	 * 商品批量同步
	 * 
	 * @return
	 * @throws Exception
	 */
	public String goodsSync() throws Exception {
		try {
			enfuService.queryGoodsSync(getCurrentOperator());
			redirectionUrl = "enfu!list.htm";
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}

		return "audit_success";
	}

	/**
	 * 订单查询
	 * 
	 * @return
	 * @throws Exception
	 */
	public String queryOrderStatusList() throws Exception {
		try {
			if (enfuQuery == null) {
				enfuQuery = new EnfuQuery();
				orderno = enfuQuery.getOrderno();
				orderno = FilterStringSpaces.checkStringHasSpace(orderno);
				enfuQuery.setOrderno(orderno);
			}
			enfuInfo = enfuService.queryOrderStatus(enfuQuery,
					getCurrentOperator());
			if (enfuInfo != null) {
				orderno = enfuInfo.getOrderno();
				if (orderno == null) {
					orderno = enfuQuery.getOrderno();
					enfuInfo.setOrderno(orderno);
				}
			}
			return "order_list";
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}

		return SUCCESS;
	}

	/**
	 * 余额查询
	 * 
	 * @return
	 * @throws Exception
	 */
	public String queryBalance() throws Exception {
		try {
			String merchantid = null;
			merchantidList = enfuService.queryMerchantid(getCurrentOperator());
			if (merchantidList != null && merchantidList.size() > 0) {
				merchantid = merchantidList.get(0).getMerchantid();
			}
			if (enfuQuery == null) {
				enfuQuery = new EnfuQuery();
				enfuQuery.setMerchantid(merchantid);
			}
			enfuInfo = enfuService
					.queryBalance(enfuQuery, getCurrentOperator());
			enfuInfo.setMerchantid(merchantid);
			if (enfuInfo != null) {
				if ("-1".equals(enfuInfo.getResultcode())) {
					flag = false;
					addActionMessage(enfuInfo.getResultdescription());
				} else if ("-2".equals(enfuInfo.getResultcode())) {
					flag = false;
					addActionMessage(enfuInfo.getResultdescription());
				} else if ("-3".equals(enfuInfo.getResultcode())) {
					flag = false;
					addActionMessage(enfuInfo.getResultdescription());
				} else {
					return "balance";
				}
			}
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}

		return "balance";
	}

	/**
	 * 批量上架
	 * 
	 * @return
	 * @throws Exception
	 */
	public String batchOnline() throws Exception {
		try {
			enfuService.batchOnline("1", getCurrentOperator());
			redirectionUrl = "enfu!goodsDetailList.htm";
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}
		return SUCCESS;
	}

	/**
	 * 批量下架
	 * 
	 * @return
	 * @throws Exception
	 */
	public String batchOffline() throws Exception {
		try {
			enfuService.batchOnline("0", getCurrentOperator());
			redirectionUrl = "enfu!goodsDetailList.htm";
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}
		return SUCCESS;
	}

	/**
	 * 商品审核
	 * 
	 * @return
	 * @throws Exception
	 */
	public String audit() throws Exception {
		// try {
		// enfuService.confirmAudit(supplierId, priceRatio,
		// getCurrentOperator());
		// redirectionUrl = "enfu!list.htm";
		// } catch (ManagerException e) {
		// flag = false;
		// addActionError(e.getMessage());
		// }
		return "audit_success";
	}

	/**
	 * 商品详细信息同步
	 * 
	 * @return
	 * @throws Exception
	 */
	public String goodsDetailSync() throws Exception {
		try {
			enfuService.querygoodsDetailSync(getCurrentOperator());
			redirectionUrl = "enfu!list.htm";
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}
		return "audit_success";
	}

	/**
	 * 分页列表显示
	 * 
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception {
		try {
			if (enfuQuery == null) {
				enfuQuery = new EnfuQuery();
				enfuQuery.setPageSize(Constants.INIT_PAGESIZE);
				enfuQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
			}
			String productid = enfuQuery.getProductid();
			String productname = enfuQuery.getProductname();
			productid = FilterStringSpaces.checkStringHasSpace(productid);
			productname = FilterStringSpaces.checkStringHasSpace(productname);
			enfuQuery.setProductid(productid);
			enfuQuery.setProductname(productname);
			page = enfuService.queryByPage(enfuQuery, getCurrentOperator());
		} catch (ManagerException e) {
			addActionError(e.getMessage());
		}
		return "goods_list";
	}

	public EnfuInfo getEnfuInfo() {
		return enfuInfo;
	}

	public void setEnfuInfo(EnfuInfo enfuInfo) {
		this.enfuInfo = enfuInfo;
	}

	public EnfuQuery getEnfuQuery() {
		return enfuQuery;
	}

	public void setEnfuQuery(EnfuQuery enfuQuery) {
		this.enfuQuery = enfuQuery;
	}

	public List<EnfuInfo> getMerchantidList() {
		return merchantidList;
	}

	public void setMerchantidList(List<EnfuInfo> merchantidList) {
		this.merchantidList = merchantidList;
	}

	public int getPriceRatio() {
		return priceRatio;
	}

	public void setPriceRatio(int priceRatio) {
		this.priceRatio = priceRatio;
	}

	public String getSupplierId() {
		return supplierId;
	}

	public void setSupplierId(String supplierId) {
		this.supplierId = supplierId;
	}

	public Pagination<EnfuInfo> getPage() {
		return page;
	}

	public void setPage(Pagination<EnfuInfo> page) {
		this.page = page;
	}

	public String getOrderno() {
		return orderno;
	}

	public void setOrderno(String orderno) {
		this.orderno = orderno;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

}
